package com.l024.meet.ui;

import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.l024.framework.base.BaseBackActivity;
import com.l024.framework.bmob.BmobManager;
import com.l024.framework.bmob.IMUser;
import com.l024.framework.model.AddFriendModel;
import com.l024.framework.utils.CommonUtils;
import com.l024.framework.utils.Logutils;
import com.l024.meet.R;
import com.l024.meet.adapter.AddFriendAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

public class ContactFriendActivity extends BaseBackActivity {

    private RecyclerView mContactView;
    private AddFriendAdapter addFriendAdapter;
    private List<AddFriendModel> addFriendModels;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contact_friend);
        initView();
    }

    private void initView() {
        mContactView = (RecyclerView) findViewById(R.id.mContactView);
        mContactView.setLayoutManager(new LinearLayoutManager(this));
        mContactView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
        addFriendModels = new ArrayList<>();
        addFriendAdapter = new AddFriendAdapter(ContactFriendActivity.this,addFriendModels);
        mContactView.setAdapter(addFriendAdapter);
        addFriendAdapter.setOnClickListener(new AddFriendAdapter.OnClickListener() {
            @Override
            public void OnClick(int position) {
                AddFriendModel addFriendModel = addFriendModels.get(position);
                if(addFriendModel.getType()==AddFriendAdapter.TYPE_CONTENT){
                    UserInfoActivity.startActivity(ContactFriendActivity.this,addFriendModel.getUserId());
                }
            }
        });
        //获取通讯录手机号
        Map<String, String> contactMap = loadContact();
        //加载用户
        loadUser(contactMap);
    }

    /**
     * 加载用户
     */
    private void loadUser(Map<String, String> contacts){
        if(contacts!=null&&contacts.size()>0){
            for(Map.Entry<String,String> entry:contacts.entrySet()){
                BmobManager.getInstance().queryPhoneFriend(entry.getKey(), new FindListener<IMUser>() {
                    @Override
                    public void done(List<IMUser> list, BmobException e) {
                        if(e==null){
                            if(CommonUtils.isEmpty(list)){
                                for (IMUser imUser:list){
                                    AddFriendModel addFriendModel = new AddFriendModel();
                                    addFriendModel.setType(AddFriendAdapter.TYPE_CONTENT);
                                    addFriendModel.setUserId(imUser.getObjectId());
                                    addFriendModel.setPhoto(imUser.getPhoto());
                                    addFriendModel.setSex(imUser.isSex());
                                    addFriendModel.setAge(imUser.getAge());
                                    addFriendModel.setNickName(imUser.getNickName());
                                    addFriendModel.setDesc(imUser.getDesc());
                                    addFriendModel.setContact(true);
                                    addFriendModels.add(addFriendModel);
                                }
                                addFriendAdapter.notifyDataSetChanged();
                            }else{
                                Logutils.i("通讯录查询好友为Null");
                            }
                        }else{
                            Logutils.e("好友查询失败"+entry.getKey()+"-------------"+e.getMessage());
                        }
                    }
                });
            }
        }
    }

    /**
     * 读取联系人
     */
    private Map<String,String> loadContact(){
        Map<String,String> contact = new HashMap<>();
        Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,null,null,null);
        String name;
        String phone;
        while (cursor.moveToNext()){
            name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            phone = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            phone = phone.replace("-","").replace(" ","");
            if(!TextUtils.isEmpty(phone)&&!TextUtils.isEmpty(name)){
                contact.put(phone,name);
            }
        }
        return contact;
    }
}
